#!/bin/bash

set -e

VERSION=$1

# 版本号检测
case "$VERSION" in
  "develop")
    ;;
  "release")
    ;;
  *) echo "版本号错误:$VERSION"
    exit 1
    ;;
esac

# 编译
layaair2-cmd publish -c web

# 参数
REGISTRY=registry.cn-hangzhou.aliyuncs.com
REGISTRY_NAMESPACE=fattigerlee
REGISTRY_USER=505179140@qq.com
REGISTRY_PASSWORD=C317t#VrjAb*r%uv

# 登录docker
docker login -u $REGISTRY_USER -p $REGISTRY_PASSWORD $REGISTRY

echo "生成Dockerfile..."

# 测试服
if [ "$VERSION" = "develop" ]; then
cat>Dockerfile<<EOF
FROM nginx

COPY release/web/ /usr/share/nginx/html
COPY nginx/develop.conf /etc/nginx/conf.d/default.conf

EOF
fi

echo "生成Dockerfile完成!!!"

echo "推送镜像..."

NAME="demo"
IMAGE_NAME=$REGISTRY/$REGISTRY_NAMESPACE/$NAME
docker build -t $NAME .

docker tag $(docker images | grep $NAME | head -1 | awk '{print $3}') $IMAGE_NAME:$VERSION
docker push $IMAGE_NAME:$VERSION

docker rmi -f $(docker images | grep ${IMAGE_NAME} | awk '{print $3}')

docker image prune -f
rm -rf Dockerfile
rm -rf release

echo "推送镜像完成!!!"